﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.IO;
using System.Threading;

/// <summary>
/// Pdf2Swf 将pdf转化为swf
/// </summary>
public class Pdf2Swf
{
    public Pdf2Swf()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public void PDFConvertToSWF(string sourcePath, string targetPath)
    {
        try
        {
          
            System.Diagnostics.Process p = new Process();

            p.StartInfo.FileName = "cmd";

            p.StartInfo.UseShellExecute = false;

            p.StartInfo.RedirectStandardInput = true;

            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.RedirectStandardError = false;

            p.StartInfo.CreateNoWindow = true;

            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            p.Start();

            string strOutput = null;
            if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("../file/pdf2swf.exe")))
            {
               // throw new Exception("路径存在");
            }
            else
            {
               // throw new Exception("路径不存在");
            }
            if (File.Exists(sourcePath))
            {
                
            }
            string pdf2swf = System.Web.HttpContext.Current.Server.MapPath("../file/pdf2swf.exe");
            string s = pdf2swf + " " + sourcePath + " -o " + targetPath + "  -f -T 9 ";

            p.StandardInput.WriteLine(s);

            p.StandardInput.WriteLine("exit");

            strOutput = p.StandardOutput.ReadToEnd();

            Console.WriteLine(strOutput);
            //p.Close();
            //Dispose();

        }

        catch (Exception ex)
        {
            throw;
        }
    }
    public static void Dispose()
    {
        // stop soffice process
        Process[] ps = Process.GetProcessesByName("pdf2swf");
        foreach (Process p in ps)
        {
            p.Kill();
        }
        Process[] ps2 = Process.GetProcessesByName("cmd");
        foreach (Process p in ps2)
        {
            p.Kill();
        }
        //if (ps != null && ps.Length > 0)
        //    ps[0].Kill();

    }

    //private void KillPs()
    //{
    //    System.Diagnostics.Process[] myPs;
    //    myPs = System.Diagnostics.Process.GetProcesses();
    //    foreach (System.Diagnostics.Process p in myPs)
    //    {
    //        if (p.Id != 0)
    //        {
    //            try//由于进程不同，有的进程不包含Modules信息，所以要用try保护               
    //            {
    //                if (p.Modules != null)
    //                    for (int i = 0; i < p.Modules.Count; i++)
    //                    {
    //                        System.Diagnostics.ProcessModule pm = p.Modules[i];
    //                        if (pm.ModuleName == "pdf2swf")
    //                        {
    //                            p.Kill();

    //                        }
    //                        if (pm.ModuleName == "cmd")
    //                        {
    //                            p.Kill();

    //                        }

    //                    }
    //            }
    //            catch { }
    //            finally
    //            {
    //            }
    //        }

    //    }
    //}
}
